using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;

using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace eSignature
{
  
    public partial class ThisAddIn
    {
        public static Microsoft.Office.Interop.Word.Document currentdoc = null;

        private Type applicationType;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            SignaturePictureToWord("123456","d:\\zhang.jpg",false,false);
            //this.Application.Selection.InlineShapes.AddPicture(@"d:\zhang.jpg",
   //ref missing, ref missing, ref missing);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            
        }

        /// <summary>
        /// word电子签章
        /// </summary>
        /// <param name="ProtectPWD">签章密码</param>
        /// <param name="SignaturePicture">签章图片</param>
        /// <param name="isSignatureCustor">签章位置，当前坐标点还是设定区域</param>
        /// <param name="isSignatureHidden">是否隐藏文档</param>
        public void SignaturePictureToWord(string ProtectPWD, string SignaturePicture, bool isSignatureCustor, bool isSignatureHidden)
        {
            //打印的代码如下：
            ////Microsoft.Office.Interop.Word.Application app = null;
            object application = this.Application;
            object applicationObject = application;
            applicationType = applicationObject.GetType();
            Microsoft.Office.Interop.Word.Document doc = null;
            Microsoft.Office.Interop.Word.Shape oShape = null;
            object missing = System.Reflection.Missing.Value;
            doc = (Microsoft.Office.Interop.Word.Document)applicationType.InvokeMember("ActiveDocument", BindingFlags.GetProperty, null, applicationObject, null);
            currentdoc = doc;
            try
            {
                if (isSignatureCustor)
                {
                    foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
                    {
                        bm.Select();
                        string item = bm.Name;
                        if (item.Equals("signature"))
                        {

                            string fileName = SignaturePicture;//签章图片
                            object LinkToFile = false;
                            object SaveWithDocument = true;
                            object Range = missing;
                            object Left = 300;
                            object Top = 0;
                            object Width = 100;
                            object Height = 100;
                            object Anchor = null;

                            Anchor = bm.Range;//插入到设定书签处
                            oShape = doc.Shapes.AddPicture(fileName, ref LinkToFile, ref SaveWithDocument, ref Left, ref Top, ref Width, ref Height, ref Anchor);
                            oShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
                            if (isSignatureHidden)
                            {
                                HideText(1);//隐藏文档
                            }
                            Object objProtectPWD = ProtectPWD;   //锁定的密码
                            Object NoSet = null;
                            doc.Protect2002(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyFormFields, ref NoSet, ref objProtectPWD);
                            doc.Save();
                            File.Delete(fileName);
                            MessageBox.Show("签章成功!");

                        }

                    }
                }
                else
                {
                    doc.Application.Selection.Range.Select();
                    float leftPosition = (float)doc.Application.Selection.get_Information(
                    Microsoft.Office.Interop.Word.WdInformation.wdHorizontalPositionRelativeToPage);




                    // 获取标题距页面边上的距离，单位为：像素wdVerticalPositionRelativeToPage
                    float topPosition = (float)doc.Application.Selection.get_Information(
                        Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage);


                    string FileName = SignaturePicture;//签章图片
                    Image img = Image.FromFile(FileName);
                    object Left = leftPosition;
                    object Top = topPosition;
                    object Width = img.Size.Width;
                    object Height = img.Size.Height;
                    object LinkToFile = false;
                    object SaveWithDocument = true;
                    object Range = missing;
                    object Anchor = doc.Application.Selection.Range;//插入到光标区域



                    doc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref  LinkToFile, ref  SaveWithDocument, ref  Anchor);
                    int count = doc.InlineShapes.Count;
                    Microsoft.Office.Interop.Word.Shape s = doc.Application.ActiveDocument.InlineShapes[count].ConvertToShape();
                    s.Height = img.Size.Height;
                    s.Width = img.Size.Width;
                    s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
                    if (isSignatureHidden)
                    {
                        HideText(1);//隐藏文档
                    }




                }
            }
            catch
            {
            }
        }

        private void HideText(int hidden)
        {
            object application = this.Application;
            object applicationObject = application;
            Microsoft.Office.Interop.Word.Document doc = null;
            doc = (Microsoft.Office.Interop.Word.Document)applicationType.InvokeMember("ActiveDocument", BindingFlags.GetProperty, null, applicationObject, null);
            object start = doc.Content.Start;
            object end = doc.Content.End;
            Microsoft.Office.Interop.Word.Range rng = doc.Range(ref start, ref end);

            rng.Select();

            rng.Font.Hidden = hidden;  // 1 = True   0 = False
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
